import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.util.LinkedList;
import java.util.List;

public class DummyModel implements IBouncingBallsModel {

	private final double areaWidth;
	private final double areaHeight;

	private Ball ball;
//	private Ball ball2;
	private Ball ball3;

	private boolean enableCollision = true;

	List<Ball> balls = new LinkedList<Ball>();

	private double gravity = -0.4;

	public DummyModel(double width, double height) {
				//Ball(xPos, yPos, xVel, yVel, radius, gravity)
		ball = new Ball(1, 5, -1.5, 0.8, 0.7, gravity);
	//	ball2 = new Ball(5, 5, 2, 2, 1, gravity, Color.GREEN);
		ball3 = new Ball(2, 4, -0.2, 0.8, 0.35, gravity);
		this.areaWidth = width;
		this.areaHeight = height;
		balls.add(ball);
	//	balls.add(ball2);
		balls.add(ball3);
	}

	@Override
	public void tick(double deltaT) {
		for (Ball b : balls) {

			b.move(deltaT);
			
			//left wall COLLISION
			if (b.getX() < b.getR()) {
				b.setVx(b.getVx() * -1);
				//System.out.println("x before:" + b.getX());
				b.setX(b.getR());
				//System.out.println("x after:" + b.getX());
			}
			
			//right wall COLLISION
			if (b.getX() > areaWidth - b.getR()) {
				b.setVx(b.getVx() * -1);
				//System.out.println("x before:" + b.getX());
				b.setX(areaWidth - b.getR());
				//System.out.println("x after:" + b.getX());
			}
			
			//BOTTOM COLLISION
			if (b.getY() < b.getR()) {
				b.setVy(b.getVy() * -1);
				//System.out.println("y before: " + b.getY());
				b.setY(b.getR());
				//System.out.println("y after: " + b.getY());
			}
			
			//TOP COLLISION
			if (b.getY() > areaHeight - b.getR()){
				b.setVy(b.getVy() * -1);
				//System.out.println("y before: " + b.getY());
				b.setY(areaHeight - b.getR());
				//System.out.println("y after: " + b.getY());
			}

			for (int i = 0; i <= balls.size() - 1; i++) {
				for (int j = i; j <= balls.size() - 1; j++) {
					if (enableCollision) {
						if (checkCollision(balls.get(i), balls.get(j)) && i != j) {

							enableCollision = false;
						//	System.out.println("collide");
							double I;
							double R;
							double v1;
							double z1;
							double w1;
							double v2;
							double z2;
							double w2;
							double firstTotalSpeed;
							double secondTotalSpeed;

							Ball first = balls.get(i);
							Ball second = balls.get(j);

							firstTotalSpeed = first.getTotalSpeed();
							secondTotalSpeed = second.getTotalSpeed();

							I = first.getMass() * first.getTotalSpeed()
									+ second.getMass() * second.getTotalSpeed();

							R = second.getTotalSpeed() - first.getTotalSpeed();

							firstTotalSpeed = (I - (second.getMass() * R))
									/ (first.getMass() + second.getMass());

							secondTotalSpeed = (I - (first.getMass() * R))
									/ (second.getMass() + first.getMass());


							double difXfirst = differenceX(first, second);
							double difYfirst = differenceY(first, second);

							double difXsecond = differenceX(second, first);
							double difYsecond = differenceY(second, first);

							v1 = Math.atan2(difYfirst, difXfirst);

							z1 = Math.atan2(first.getVy(), first.getVx());

							w1 = z1 - v1;

							v2 = Math.atan2(difYsecond, difXsecond);

							z2 = Math.atan2(second.getVy(), second.getVx());

							w2 = z2 - v2;
							
							first.setVx(Math.cos(w1) * firstTotalSpeed);

							first.setVy(Math.sin(w1) * firstTotalSpeed);

							second.setVx(Math.cos(w2) * secondTotalSpeed);

							second.setVy(Math.sin(w2) * secondTotalSpeed);
							
							

						}

					}

					
					if(!checkCollision(balls.get(i), balls.get(j)) && i != j && enableCollision == false){
						enableCollision = true;
					//	System.out.println("spokKy");
					}
				}
			}
		}
	}

	private double differenceX(Ball first, Ball second) {
		double angleX = first.getX() - second.getX();
		return angleX;
	}

	private double differenceY(Ball first, Ball second) {
		double angleY = first.getY() - second.getY();
		return angleY;
	}

	@Override
	public List<Ellipse2D> getBalls() {
		List<Ellipse2D> myBalls = new LinkedList<Ellipse2D>();
		for (Ball b : balls) {
			myBalls.add(new Ellipse2D.Double(b.getX() - b.getR(), b.getY()
					- b.getR(), 2 * b.getR(), 2 * b.getR()));
		}
		return myBalls;
	}

	public List<Ball> getBallList() {
		return balls;
	}

	public Boolean checkCollision(Ball first, Ball second) {
		if (Math.sqrt(Math.pow((first.getX() - second.getX()), 2)
				+ Math.pow((first.getY() - second.getY()), 2)) <= (first.getR() + second
						.getR())) {
			return true;
		}
		return false;
	}
}
